package com.sms.biz.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.sms.biz.IOperlogBiz;
import com.sms.dao.IOperlogDAO;
import com.sms.po.Operlog;


/**
 * @author 20111009 by cxh
 */
public class OperlogBizImpl implements IOperlogBiz {
	private IOperlogDAO ibld = null;
	public void setIbld(IOperlogDAO ibld){ this.ibld = ibld; }
	@Override
	public List<Operlog> showAllOperlog() {
		return ibld.showAllOperlog();
	}

	@Override
	public List<Operlog> showAllOperlogByPage(String userId, int start, int limit) {
		// 离线查询
		DetachedCriteria criteria = DetachedCriteria
				.forClass(Operlog.class);
		// 如果条件标题不为空,那么将条件标题附加上
		if (!"".equals(userId) && null != userId) {
			criteria.add(Restrictions.like("userId", userId,
					MatchMode.ANYWHERE));
		}
		criteria.addOrder(Order.desc("id"));
		if (start <= 0) {start = 0;		}
		if (limit <= 0) {limit = 10;	}
		return ibld.showAllOperlogByPage(criteria, start, limit);
	}
	@Override
	public int getAllOperlogCount(String userId) {
		// 离线查询
		DetachedCriteria criteria = DetachedCriteria
				.forClass(Operlog.class);
		// 如果条件标题不为空,那么将条件标题附加上
		if (!"".equals(userId) && null != userId) {
			criteria.add(Restrictions.like("userId", userId,
					MatchMode.ANYWHERE));
		}
		return ibld.getAllOperlogCount(criteria);
	}
	@Override
	public boolean addOperlog(String userId, String logtype, String content) {
		String remark = "";
		return this.addOperlog(userId, logtype, content, remark);
	}
	@Override
	public boolean addOperlog(String userId, String logtype, String content, String remark) {
		try {
			Operlog ol = new Operlog();
			ol.setContent(content);
			ol.setUserId(userId);
			ol.setLogtype(logtype);
			if (!"".equals(remark)&&null != remark){
				ol.setRemark(remark);
			}
			ol.setCreateDate(new Date());
			ibld.addOperlog(ol);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
